package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.book.model.entity.PointsRecords;
import com.book.mapper.PointsRecordsMapper;
import com.book.model.entity.User;
import com.book.service.PointsRecordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 积分记录表 服务实现类
 * </p>
 *
 * @author auto
 * @since 2025-04-02 06:38:55
 */
@Service
public class PointsRecordsServiceImpl extends ServiceImpl<PointsRecordsMapper, PointsRecords> implements PointsRecordsService {

    @Autowired
    private UserService userService;
    @Override
    public List<PointsRecords> getPointsRecordsListByUserId(Integer type,String userId) {
        LambdaQueryWrapper<PointsRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userId),PointsRecords::getUserId, userId);
        queryWrapper.eq(type!=null,PointsRecords::getRecordsType, type);
        queryWrapper.orderByDesc(PointsRecords::getCreateTime);
        return this.list(queryWrapper);
    }

    @Override
    public Integer userSignArrive(String userId) {
        PointsRecords pointsRecords = new PointsRecords();
        pointsRecords.setUserId(userId);
        pointsRecords.setRecordsType(0);
        pointsRecords.setDescription("签到奖励");
        pointsRecords.setPointsAmount(10L);
        pointsRecords.setCreateTime(new Date());
        this.save(pointsRecords);

        User user = userService.getById(userId);
        user.setPoints(user.getPoints()+10);
        userService.updateById(user);
        return 10;
    }

    @Override
    public boolean checkSign(String userId) {
        //先查询用户今天有没有签到
        LambdaQueryWrapper<PointsRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userId),PointsRecords::getUserId, userId);
        queryWrapper.eq(PointsRecords::getDescription, "签到奖励");

        // 获取今天的开始时间和结束时间
        Date currentDate = new Date();
        Date startOfDay = this.getStartOfDay(currentDate); // 今天的开始时间（00:00:00）
        Date endOfDay = this.getEndOfDay(currentDate);     // 今天的结束时间（23:59:59）

        // 添加日期范围查询
        queryWrapper.between(PointsRecords::getCreateTime, startOfDay, endOfDay);
        List<PointsRecords> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            return true;
        }
        return false;
    }

    @Override
    public boolean buyBook(String userId, Long amount,String bookName) {
        PointsRecords pointsRecords = new PointsRecords();
        pointsRecords.setUserId(userId);
        pointsRecords.setRecordsType(1);
        pointsRecords.setDescription("购买书籍【"+bookName+"】");
        pointsRecords.setPointsAmount(amount);
        pointsRecords.setCreateTime(new Date());
        return this.save(pointsRecords);
    }

    @Override
    public boolean sellBook(String userId, Long amount,String bookName) {
        PointsRecords pointsRecords = new PointsRecords();
        pointsRecords.setUserId(userId);
        pointsRecords.setRecordsType(0);
        pointsRecords.setDescription("出售书籍【"+bookName+"】");
        pointsRecords.setPointsAmount(amount);
        pointsRecords.setCreateTime(new Date());
        return this.save(pointsRecords);
    }

    @Override
    public boolean sellerCancelBook(String userId, Long amount, String bookName) {
        PointsRecords pointsRecords = new PointsRecords();
        pointsRecords.setUserId(userId);
        pointsRecords.setRecordsType(1);
        pointsRecords.setDescription("取消出售书籍【"+bookName+"】");
        pointsRecords.setPointsAmount(amount);
        pointsRecords.setCreateTime(new Date());
        return this.save(pointsRecords);
    }

    @Override
    public boolean buyerCancelBook(String userId, Long amount, String bookName) {
        PointsRecords pointsRecords = new PointsRecords();
        pointsRecords.setUserId(userId);
        pointsRecords.setRecordsType(0);
        pointsRecords.setDescription("卖家取消出售书籍【"+bookName+"】");
        pointsRecords.setPointsAmount(amount);
        pointsRecords.setCreateTime(new Date());
        return this.save(pointsRecords);
    }

    @Override
    public boolean pushPoints(PointsRecords dto) {
        dto.setRecordsType(0);
        dto.setDescription("管理员发放积分");
        dto.setCreateTime(new Date());
        User user = userService.getById(dto.getUserId());
        user.setPoints(user.getPoints()+dto.getPointsAmount());
        userService.updateById(user);
        return this.save(dto);
    }


    // 获取某一天的开始时间（00:00:00）
    private Date getStartOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取某一天的结束时间（23:59:59）
    private Date getEndOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
}
